package com.choichumgming.rentcat.config;

import com.choichumgming.rentcat.dto.QueueEnum;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName RabbitMqConfig
 * @Description : 消息队列配置

 * @Author : choichumgming
 * @Date : 5/3/2020 16:32
*/
@Configuration
public class RabbitMqConfig {
    @Bean
    DirectExchange orderDirect() {
        return (DirectExchange) ExchangeBuilder.directExchange(QueueEnum.QUEUE_ORDER_CANCEL.getExchange()).durable(true).build();
    }
    @Bean
    DirectExchange orderTtlDirect() {
        return (DirectExchange) ExchangeBuilder.directExchange(QueueEnum.QUEUE_TTL_ORDER_CANCEL.getExchange()).durable(true).build();
    }
    @Bean
    public Queue orderQueue() {
        return new Queue(QueueEnum.QUEUE_ORDER_CANCEL.getName());
    }

    @Bean
    public Queue orderTtlQueue() {
        return QueueBuilder
                .durable(QueueEnum.QUEUE_TTL_ORDER_CANCEL.getName())
                .withArgument("x-dead-letter-exchange", QueueEnum.QUEUE_ORDER_CANCEL.getExchange())
                .withArgument("x-dead-letter-routing-key", QueueEnum.QUEUE_ORDER_CANCEL.getRouteKey()).build();
    }

    @Bean
    Binding orderBinding(DirectExchange orderDirect,Queue orderQueue){
        return BindingBuilder.bind(orderQueue).to(orderDirect).with(QueueEnum.QUEUE_ORDER_CANCEL.getRouteKey());
    }

    @Bean
    Binding orderTtlBinding(DirectExchange orderTtlDirect,Queue orderTtlQueue){
        return BindingBuilder.bind(orderTtlQueue).to(orderTtlDirect).with(QueueEnum.QUEUE_TTL_ORDER_CANCEL.getRouteKey());
    }

}
//
//@Configuration
//public class RabbitMqConfig {
//
//    /**
//     * 订单消息实际消费队列所绑定的交换机
//     */
//    @Bean
//    DirectExchange orderDirect() {
//        return (DirectExchange) ExchangeBuilder
//                .directExchange(QueueEnum.QUEUE_ORDER_CANCEL.getExchange())
//                .durable(true)
//                .build();
//    }
//
//    /**
//     * 订单延迟队列队列所绑定的交换机
//     */
//    @Bean
//    DirectExchange orderTtlDirect() {
//        return (DirectExchange) ExchangeBuilder
//                .directExchange(QueueEnum.QUEUE_TTL_ORDER_CANCEL.getExchange())
//                .durable(true)
//                .build();
//    }
//
//    /**
//     * 订单实际消费队列
//     */
//    @Bean
//    public Queue orderQueue() {
//        return new Queue(QueueEnum.QUEUE_ORDER_CANCEL.getName());
//    }
//
//    /**
//     * 订单延迟队列（死信队列）
//     * x-message-ttl ：消息超时时间，单位为毫秒
//     * x-dead-letter-exchange : 死信交换机
//     * x-dead-letter-routing-key：死信RouteKey
//     */
//    @Bean
//    public Queue orderTtlQueue() {
//        return QueueBuilder
//                .durable(QueueEnum.QUEUE_TTL_ORDER_CANCEL.getName())
////                .withArgument("x-message-ttl", 60*1000)//到期后转发的交换机
//                .withArgument("x-dead-letter-exchange", QueueEnum.QUEUE_ORDER_CANCEL.getExchange())//到期后转发的交换机
//                .withArgument("x-dead-letter-routing-key", QueueEnum.QUEUE_ORDER_CANCEL.getRouteKey())//到期后转发的路由键
//                .build();
//    }
//
//    /**
//     * 将订单队列绑定到交换机
//     */
//    @Bean
//    Binding orderBinding(DirectExchange orderDirect,Queue orderQueue){
//        return BindingBuilder
//                .bind(orderQueue)
//                .to(orderDirect)
//                .with(QueueEnum.QUEUE_ORDER_CANCEL.getRouteKey());
//    }
//
//    /**
//     * 将订单延迟队列绑定到交换机
//     */
//    @Bean
//    Binding orderTtlBinding(DirectExchange orderTtlDirect,Queue orderTtlQueue){
//        return BindingBuilder
//                .bind(orderTtlQueue)
//                .to(orderTtlDirect)
//                .with(QueueEnum.QUEUE_TTL_ORDER_CANCEL.getRouteKey());
//    }
//
//}
